﻿using MvcSmartTool.Common;
using MvcSmartTool.DomainGenerator;
using MvcSmartTool.ServiceGenerator;
using MvcSmartTool.WebGenerator;
using SampleDomainDll;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MvcSmartTool {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public ProjectModel Model { get; set; }
        private List<IProjectWriter> ProjectWriters { get; set; }
        public MainWindow() {
            InitializeComponent();
            Model = new ProjectModel() {
                ProjectName = "SampleProject",
                ProjectOutput = "./SampleProject"
            };
            DataContext = Model;
            ProjectWriters = new List<IProjectWriter>();
            Model.PropertyChanged += ProjectConfigChanged;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProjectConfigChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
            ProjectWriters.Clear();
            var domainProject = new DomainProjectWriter(Model.ProjectName,Model.ProjectOutput);
            ProjectWriters.Add(domainProject);
            var serviceProject = new ServiceProjectWriter(Model.ProjectName, Model.ProjectOutput);
            ProjectWriters.Add(serviceProject);
            var webProject = new WebProjectWriter(Model.ProjectName, Model.ProjectOutput);
            ProjectWriters.Add(webProject);
            //TODO: add more project to generate
        }

        private void StartGenerate(object sender, RoutedEventArgs e) {
            foreach (var project in ProjectWriters) {
                project.Models = Model.Types.ToList();
                project.Generate();
            }
            //TODO: Copy package folder
            
            ////
            System.Windows.MessageBox.Show("Finish");
        }

        private void GetModels() {
            //TODO: implement by get all type by attributes in dll...
            //return new List<TypeGenerationModel> {
            //    new TypeGenerationModel(typeof(UserProfile), Model.ProjectName),
            //    new TypeGenerationModel(typeof(Blog), Model.ProjectName),
            //    new TypeGenerationModel(typeof(Comment), Model.ProjectName)
            //};
            var rs = new List<TypeGenerationModel>();
            Model.Types.Clear();
            var assembly = System.Reflection.Assembly.LoadFrom(Model.DllPath);
            var types = assembly.GetTypes();
            foreach (var type in types) {
                Model.Types.Add(new TypeGenerationModel(type, Model.ProjectName));
            }
        }

        private void OpenOutputDirectory(object sender, RoutedEventArgs e) {
            var dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                Model.ProjectOutput = dialog.SelectedPath;
            }
        }

        private void OnSelectDllPath(object sender, RoutedEventArgs e) {
            var openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Dll (*.dll)|*.dll";
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                Model.DllPath = openFileDialog.FileName;
                GetModels();
            }
        }
    }
}
